package org.example.spring.carsalemanagementsystem.controller;

import lombok.RequiredArgsConstructor;
import org.example.spring.carsalemanagementsystem.entity.Commodity;
import org.example.spring.carsalemanagementsystem.service.impl.CommodityServiceImpl;
import org.example.spring.carsalemanagementsystem.utils.Page;
import org.example.spring.carsalemanagementsystem.utils.Result;
import org.example.spring.carsalemanagementsystem.vo.CommodityFuzzyVo;
import org.example.spring.carsalemanagementsystem.vo.CommodityVo;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/commodity")
@RequiredArgsConstructor
public class CommodityController {
    private final CommodityServiceImpl commodityService;
    @GetMapping("/findPages")
    public Result findPages(@RequestParam(required = false)Long pageNum,
                            @RequestParam(required = false)Long pageSize){
        Page<Commodity> pages = commodityService.findPages(pageNum, pageSize);
        return Result.success().put(pages);
    }
    @GetMapping("/findList")
    public Result findList(){
        List<CommodityVo> list = commodityService.findList();
        return Result.success().put(list);
    }
    @GetMapping("/findListByKey")
    public Result findListByKey(@RequestParam("key")String key){
        List<CommodityVo> list = commodityService.findListByKey(key);
        return Result.success().put(list);
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody Commodity commodity){
        commodity.setCreatetime(LocalDateTime.now());
        commodityService.insertOne(commodity);
        return Result.success();
    }
    @PostMapping("/delete")
    public Result delete(String carid){
        commodityService.deleteById(carid);
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Commodity commodity){
        commodityService.updateById(commodity);
        return Result.success();
    }
    @PostMapping("/fuzzy")
    public Result fuzzy(@RequestBody CommodityFuzzyVo vo){
        Page<Commodity> pages = commodityService.findPagesByKey(vo);
        return Result.success().put(pages);
    }
}
